<template>
  <div class="login">
    <div class="login-content">
      <div class="site-name padding-lr-sm">
          <div class="name">辅学院</div>
          <div class="sub-name">企业培训系统</div>
      </div>
      <div class="login-container">
        <div class="welcome padding-lr-sm">Hi，欢迎登录!</div>
        <van-form @submit="confirmlogin" label-width="0">
          <van-field
            size="large"
            label=""
            left-icon="contact"
            v-model="form.username"
            placeholder="请输入用户名"
            :rules="[{ required: true, message: '' }]"
          >
            <template #left-icon>
              <van-image :src="require('@/assets/login/user.png')" class="left-icon" />
            </template>
          </van-field>
          <van-field
            size="large"
            label=""
            type="password"
            v-model="form.password"
            placeholder="请输入密码"
            right-icon="eye-o"
            :rules="[{ required: true, message: '' }]"
            v-if="!isShowPassword"
            @click-right-icon="isShowPassword = true"
          >
            <template #left-icon>
              <van-image :src="require('@/assets/login/lock.png')" class="left-icon" />
            </template>
          </van-field>
          <van-field
            size="large"
            label=""
            right-icon="closed-eye"
            v-model="form.password"
            placeholder="请输入密码"
            :rules="[{ required: true, message: '' }]"
            v-if="isShowPassword"
            @click-right-icon="isShowPassword = false"
          >
            <template #left-icon>
              <van-image :src="require('@/assets/login/lock.png')" class="left-icon" />
            </template>
          </van-field>
          <div class="padding-lr-sm padding-tb-sm btn">
            <div class="red error" v-if="error">
              <van-icon name="warning" />
              {{ error }}
            </div>
            <van-button native-type="submit" type="info" block round>登录</van-button>
          </div>
        </van-form>
      </div>
    </div>

  </div>
</template>

<script>
import { login } from '@/api/user'
export default {
  name: 'login',
  data() {
    return {
      form: {
        username: 'admin',
        password: '123456'
      },
      isShowPassword: false,
      error: '',
      bottomIsShow: true, //控制底部tabbar显示与隐藏
      defaultPhoneHeight: 0, //屏幕默认高度
      nowPhoneHeight: 0, //屏幕现在的高度
      type: 'login',
      companyList: [],
      code: '',
      isDevelp: '' // 判断是否是测试环境
    }
  },
  created() {
    this.docmHeight = document.documentElement.clientHeight
  },
  mounted() {
    // 判断是否是测试环境
    this.isDevelp = process.env.NODE_ENV == 'development' ? true : false
    this.defaultPhoneHeight = window.innerHeight
    window.onresize = () => {
      //这个方法会被调用两次,软键盘弹出后和软件收起后
      this.nowPhoneHeight = window.innerHeight
    }
  },
  watch: {
    nowPhoneHeight: function () {
      if (this.defaultPhoneHeight != this.nowPhoneHeight) {
        // 当软键盘弹出,改变bottomIsSHow变量然后去控制bottom的显示与隐藏
        this.bottomIsShow = false
      } else {
        // 相等代表软键盘收起后,与上同理
        this.bottomIsShow = true
      }
    }
  },
  destroyed() {
    //页面销毁前要记得这个监听消除掉;
    window.onresize = null
  },
  methods: {

    confirmlogin() {
      console.log('登录')
      login({
        username: this.form.username,
        password: this.form.password
      })
        .then(res => {
          window.localStorage.setItem('AuthorizationToken', res.data.token)
          window.localStorage.setItem('companyId', res.data.companyId)
          window.localStorage.setItem('userInfo', JSON.stringify(res.data))
          this.$router.push({
            path: '/'
          })
        })
        .catch(err => {
          console.log(err)
          this.error = err.msg
        })
    },

  }
}
</script>

<style scoped lang="less">
@import 'index';

.login-nosee-cont {
  width: 100%;
  height: 150px;
  display: flex;
  align-items: center;
  justify-content: center;
  .tips {
    font-size: 16px;
    color: #fff;
  }
}
</style>
